<!--
 * @Author: Bennty
 * @Date: 2024-11-12 16:28:27
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-11-28 16:44:03
 * @Description: 请填写简介
-->
<template>
    <div class="p-2">
        <transition
            :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave"
        >
            <div class="search">
                <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="70px" >
                    <el-form-item label="制单日期" prop="createTime">
                        <el-date-picker v-model="queryParams.createTime" placeholder="制单日期" format="YYYY/MM/DD" value-format="YYYY-MM-DD"></el-date-picker>
                    </el-form-item>
                    <el-form-item label="生产状态" prop="produceStatus">
                        <el-select v-model="queryParams.produceStatus" placeholder="请选择">
                            <el-option label="未生产" value="0"></el-option>
                            <el-option label="已生产" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="编辑状态" prop="editStatus" placeholder="请选择">
                        <el-select v-model="queryParams.editStatus">
                            <el-option label="未编辑" value="0"></el-option>
                            <el-option label="已编辑" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="采购状态" prop="purchaseStatus" placeholder="请选择">
                        <el-select v-model="queryParams.purchaseStatus">
                            <el-option label="未采购" value="0"></el-option>
                            <el-option label="已采购" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="入库状态" prop="inStatus" placeholder="请选择">
                        <el-select v-model="queryParams.purchaseStatus">
                            <el-option label="未入库" value="0"></el-option>
                            <el-option label="部分入库" value="1"></el-option>
                            <el-option label="已入库" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="模糊搜索" prop="semiProductName">
                        <el-input v-model="queryParams.semiProductName" placeholder="模糊搜索"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <template #header>
                <el-row :gutter="8" class="mb8">
                    <el-col :span="1.5">
                        <h2>订单生产总量：{{ orderNumber }}</h2>
                    </el-col>
                </el-row>
            </template>
            <el-table :data="semiProductList" :loading="loading">
                <el-table-column label="制单员" prop="creatorName" align="center"></el-table-column>
                <el-table-column label="生产单号" prop="semiProductionCode" align="center"></el-table-column>
                <el-table-column label="制单日期" prop="createTime" align="center"></el-table-column>
                <el-table-column label="交货日期" prop="deliveryDate" align="center"></el-table-column>
                <el-table-column label="半成品名称" prop="semiProductName" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="库存数量" prop="inventoryNumber" align="center"></el-table-column>
                <el-table-column label="生产数量" prop="produceNumber" align="center"></el-table-column>
                <el-table-column label="入库数量" prop="inNumber" align="center"></el-table-column>
                <el-table-column label="生产状态" align="center">
                    <template #default="scope">
                        <span class="alert" v-if="scope.row.produceStatus == '0'">未生产</span>
                        <span class="normal" v-if="scope.row.produceStatus == '1'">已生产</span>
                    </template>
                </el-table-column>
                <el-table-column label="编辑状态" align="center">
                    <template #default="scope">
                        <span class="alert" v-if="scope.row.editStatus == '0'">未编辑</span>
                        <span class="normal" v-if="scope.row.editStatus == '1'">已编辑</span>
                    </template>
                </el-table-column>
                <el-table-column label="采纸状态" align="center">
                    <template #default="scope">
                        <el-button link type="danger" class="alert" @click="handleClickPurchasePaper(scope.row)" v-if="scope.row.purchaseStatus == '0'">未采纸</el-button>
                        <span class="normal" v-if="scope.row.purchaseStatus == '1'">已采纸</span>
                        <span class="normal" v-if="scope.row.purchaseStatus == '2'">已调拨</span>
                    </template>
                </el-table-column>
                <el-table-column label="入库状态" align="center">
                    <template #default="scope">
                        <span class="alert" v-if="scope.row.inStatus == '0'">未入库</span>
                        <span class="normal" v-if="scope.row.inStatus == '1'">部分入库</span>
                        <span class="normal" v-else-if="scope.row.inStatus == '2'">已入库</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="220px">
                    <template #default="scope">
                        <el-tooltip content="编辑" placement="top">
                            <el-button link :style="{fontSize:'15px'}" type="primary" icon="Edit" @click="handleClickEdit(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="订单详情">
                            <el-button link :style="{fontSize:'15px'}" type="warning" icon="Document" @click="handleClickCheckOrder(scope.row)" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="复制订单">
                            <el-button link :style="{fontSize:'15px'}" type="success" icon="CopyDocument" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除">
                            <el-button link :style="{fontSize:'15px'}" type="danger" icon="Delete" @click="handleClickDelete(scope.row)" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="成本分析">
                            <el-button link :style="{fontSize:'15px'}" icon="Coin" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="查看调拨详情">
                            <el-button link type="primary" :style="{fontSize:'15px'}" icon="Reading" @click="handleClickCheckAllocation(scope.row)" ></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                @pagination="getSemiProductOrderList" />
        </el-card>
        <editSemiProduct :visible="editOrderVisible" :entrepot="entrepot_type" :orderDetail="orderDetail" @close="handleCloseEdit" @submit="handleSubmit" />
        <orderDetails :visible="checkOrderVisible" :orderDetail="orderDetail" @close="handleCloseCheck" />
        <purchasePaper :visible="purchasePaperVisible" :paperInfo="paperInfo" :purchasePaperObj="purchasePaperObj" :paperType="paper_type" @submit="getSemiProductOrderList" @close="handleClosePurchasePaper" />
        <checkAllocation :visible="checkAllocationVisible" :allocationInfo="allocationInfo" @close="handleCloseCheckAllocation" />
    </div>
</template>

<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import { QuerySemiProductrderType, SemiProduceOrderVO } from '@/api/sj/productManagement/semiProductOrder/types';
import { getSemiProductOrderApi,getSemiProductOrderDetails,checkOrderDetailApi, getPaperInfoApi, deleteSemiProductOrderApi, checkPaperUseApi } from '@/api/sj/productManagement/semiProductOrder/index';
import editSemiProduct from './components/editSemiProduct.vue';
import orderDetails from './components/orderDetails.vue';
import purchasePaper from './components/purchasePaper.vue';
import checkAllocation from './components/checkAllocation.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { paper_type,entrepot_type } = toRefs<any>(proxy?.useDict('paper_type','entrepot_type'));

const queryFormRef = ref<FormInstance>();

const data = reactive<pageDataOnlyQuery<QuerySemiProductrderType>>({
    queryParams:{
        pageNum: 1,
        pageSize: 30,
        createTime: undefined,
        produceStatus: undefined,
        editStatus: undefined,
        purchaseStatus: undefined,
        inStatus: undefined,
        semiProductName: undefined
    }
});

const { queryParams } = toRefs(data);
const semiProductList = ref<SemiProduceOrderVO[]>();
const totalData = ref(0);
const loading = ref(false);
const orderNumber = ref(0);
const orderDetail = ref<any>();
const editOrderVisible = ref(false);
const checkOrderVisible = ref(false);
const purchasePaperVisible = ref(false);
const paperInfo = ref<any>();
const checkAllocationVisible = ref(false);
const allocationInfo = ref<any>();
const purchasePaperObj = ref();

const getSemiProductOrderList = async () => {
    loading.value = true;
    const result = await getSemiProductOrderApi(queryParams.value);
    //@ts-ignore
    semiProductList.value = result.dataPage.records;
    //@ts-ignore
    totalData.value = result.dataPage.total;
    //@ts-ignore
    orderNumber.value = result.orderProduceNumber;
    loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getSemiProductOrderList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};

const handleSubmit = (result: boolean) => {
    if(result){
        proxy?.$modal.msgSuccess('成功');
        getSemiProductOrderList();
    }
};

const handleClickEdit = async (row:SemiProduceOrderVO) => {
    const {code, data} = await getSemiProductOrderDetails(row.id);
    if(code == 200){
        orderDetail.value = data;
        editOrderVisible.value = true;
    }
};

const handleCloseEdit = () => {
    editOrderVisible.value = false;
};

const handleClickCheckOrder = async (row: SemiProduceOrderVO) => {
    const {code, data} = await checkOrderDetailApi(row.id);
    if(code == 200){
        orderDetail.value = data;
        checkOrderVisible.value = true;
    };
};

const handleCloseCheck = () => {
    checkOrderVisible.value = false;
};

const handleClickPurchasePaper = async (row: SemiProduceOrderVO) => {
    const {code, data} = await getPaperInfoApi(row.id);
    if(code == 200){
        paperInfo.value = data[0];
        purchasePaperObj.value = row;
        purchasePaperVisible.value = true;
    };
};

const handleClosePurchasePaper = () => {
    purchasePaperVisible.value = false;
};

const handleClickDelete = async (row: SemiProduceOrderVO) => {
    await proxy?.$modal.confirm("此条半成品生产单信息将彻底删除，确认么？");
    const {code} = await deleteSemiProductOrderApi(row.id);
    if(code == 200){
        proxy?.$modal.msgSuccess("删除半成品生产单成功！");
        getSemiProductOrderList();
    }
};

const handleClickCheckAllocation = async (row: SemiProduceOrderVO) => {
    const {code, data} = await checkPaperUseApi(row.id);
    if(code == 200){
        allocationInfo.value = data;
        checkAllocationVisible.value = true;
    };
};

const handleCloseCheckAllocation = () => {
    checkAllocationVisible.value = false;
};

onMounted(()=>{
    getSemiProductOrderList();
})

</script>

<style lang="scss" scoped>

</style>